package com.wangzhenyu.learn.test.designPattern


//命令模式

@Suppress("UNUSED")
object OCommandPattern {


    interface Command {

        fun execute()

        fun undo()
    }

    class LightOnCommand(private val light: LightReceiver) : Command {

        override fun execute() {
            light.on()
        }

        override fun undo() {
            light.off()
        }
    }

    class LightOffCommand(private val light: LightReceiver) : Command {

        override fun execute() {
            light.off()
        }

        override fun undo() {
            light.on()
        }

    }

    class NoCommand : Command {

        override fun execute() {

        }

        override fun undo() {

        }
    }

    class LightReceiver {

        fun on() {
            println("电灯打开了")
        }

        fun off() {
            println("电灯关闭了")
        }

    }

    class RemoteController {


        private val onCommands =
            mutableListOf<Command>(NoCommand(), NoCommand(), NoCommand(), NoCommand(), NoCommand())

        private val offCommands =
            mutableListOf<Command>(NoCommand(), NoCommand(), NoCommand(), NoCommand(), NoCommand())

        private var undoCommand: Command? = null

        fun setCommand(no: Int, onCommand: Command, offCommand: Command) {
            onCommands[no] = onCommand
            offCommands[no] = offCommand
        }

        fun onButtonWasPushed(no: Int) {
            onCommands[no].execute()
            undoCommand = onCommands[no]
        }

        fun offButtonWasPushed(no: Int) {
            offCommands[no].execute()
            undoCommand = offCommands[no]
        }

        fun undoButtonWasPushed() {
            undoCommand?.undo()
        }
    }

}